package cn.weekimwee.firstlineofcodeforkotlin.unit7

import android.Manifest
import android.content.pm.PackageManager
import android.os.Bundle
import android.provider.ContactsContract
import android.support.v4.app.ActivityCompat
import android.support.v4.content.ContextCompat
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import cn.weekimwee.firstlineofcodeforkotlin.BaseActivity
import cn.weekimwee.firstlineofcodeforkotlin.R
import kotlinx.android.synthetic.main.activity_contact.*
import kotlinx.android.synthetic.main.item_news.view.*
import org.jetbrains.anko.toast

class ContactActivity : BaseActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_contact)
        recyclerView.apply {
            layoutManager = LinearLayoutManager(this@ContactActivity)
            adapter = ContactAdapter()
        }
        if(ContextCompat.checkSelfPermission(this@ContactActivity,Manifest.permission.READ_CONTACTS)!=PackageManager.PERMISSION_GRANTED){
            ActivityCompat.requestPermissions(this@ContactActivity, arrayOf(Manifest.permission.READ_CONTACTS),1)
        }else{
            readContacts()
        }

    }

    override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults)
        when(requestCode){
            1->{
                if(grantResults.isNotEmpty()&&grantResults[0]==PackageManager.PERMISSION_GRANTED){
                    readContacts()
                }else{
                    toast("不给权限")
                }
            }
        }
    }

    private fun readContacts(){
        val contactList = arrayListOf<String>()
        val cursor = contentResolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,null,null,null)
        if(cursor!=null){
            while(cursor.moveToNext()){
                val name = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME))
                val num = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER))
                contactList.add("$name : $num")
            }
            (recyclerView.adapter as ContactAdapter).data = contactList
        }
        cursor.close()
    }

    class ContactAdapter : RecyclerView.Adapter<ContactAdapter.ViewHolder>() {
        var data: MutableList<String>? = null
            set(value) {
                field =value
                notifyDataSetChanged()
            }

        override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): ViewHolder {
            return ViewHolder(LayoutInflater.from(parent?.context).inflate(R.layout.item_news, parent, false))
        }

        override fun getItemCount(): Int {
            return data?.size?:0
        }

        override fun onBindViewHolder(holder: ViewHolder?, position: Int) {
            holder?.itemView?.news_title?.text = data?.get(position)
        }

        class ViewHolder(view: View) : RecyclerView.ViewHolder(view)
    }
}
